package com.wlcorp.forcazamis.web.action.system;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wlcorp.forcazamis.business.baseinfo.ICustomerService;
import com.wlcorp.forcazamis.business.system.IStaffService;
import com.wlcorp.forcazamis.domain.baseinfo.Customer;
import com.wlcorp.forcazamis.domain.baseinfo.Depot;
import com.wlcorp.forcazamis.domain.system.Department;
import com.wlcorp.forcazamis.domain.system.Role;
import com.wlcorp.forcazamis.domain.system.Staff;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.ContextUtil;
import com.wlcorp.forcazamis.sys.util.Pagination;
import com.wlcorp.forcazamis.sys.util.SessionValue;
import com.wlcorp.forcazamis.sys.util.WebUtil;
public class StaffAction extends DispatchAction {
	
	public ActionForward staffManager(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		List<Department> departmentList = ContextUtil.getInstance().getDepartmentService().getDepartmentList(null);
		request.setAttribute("departmentList",departmentList);
		return mapping.findForward("manager");
	}
	
	public ActionForward getStaffList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Staff staff = new Staff();
		WebUtil.setRequestToObject(request,staff);
		IStaffService service = ContextUtil.getInstance().getStaffService();
		int page = 1;
		int count = 0;
	    if (null != request.getParameter(Pagination.PAGE_ATTRIBUTE)) {
	      page = Integer.parseInt(request.getParameter(Pagination.
	          PAGE_ATTRIBUTE));
	    }
	    count = (int)service.getStaffCount(staff).longValue();
	    Pagination pagination = new Pagination();
	    pagination.setPageCount(Pagination.PAGE_RECORD_LINE_NUM);
	    pagination.generatePageJSPFragment(request, count);
	    staff.setPage(page, Pagination.PAGE_RECORD_LINE_NUM);
		List<Staff> staffList = service.getStaffList(staff);
		request.setAttribute("staffList",staffList);
		return mapping.findForward("list");
	}
	
	public ActionForward getStaff(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Staff staff = new Staff();
		WebUtil.setRequestToObject(request,staff);
		staff = ContextUtil.getInstance().getStaffService().getStaffById(staff.getStaffId());
		if(null == staff){
			staff = new Staff();
		}
		List<Role> roleList = ContextUtil.getInstance().getRoleService().getRoleList(null);
		List<Depot> depotList = ContextUtil.getInstance().getDepotService().getDepotList(null);
		List<Department> departmentList = ContextUtil.getInstance().getDepartmentService().getDepartmentList(null);
		List<Staff> staffList = ContextUtil.getInstance().getStaffService().getStaffList(new Staff());
		request.setAttribute("roleList",roleList);
		request.setAttribute("depotList",depotList);
		request.setAttribute("departmentList",departmentList);
		request.setAttribute("staffList",staffList);
		request.setAttribute("staff",staff);
		return mapping.findForward("modify");
	}
	
	public ActionForward modifyStaff(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Staff staff = new Staff();
		WebUtil.setRequestToObject(request,staff);
		staff.setFlag(AppConstant.RECORD_UNDELETED);
		ContextUtil.getInstance().getStaffService().saveStaff(staff);
		if(AppConstant.BASE_BEAN_STATUS_ERROR == staff.getStatus()){
			WebUtil.setAlertMessage(request,staff.getMsg());
		}else{
			WebUtil.setAlertMessage(request,"操作成功！");
		}
		
		List<Role> roleList = ContextUtil.getInstance().getRoleService().getRoleList(null);
		List<Depot> depotList = ContextUtil.getInstance().getDepotService().getDepotList(null);
		List<Department> departmentList = ContextUtil.getInstance().getDepartmentService().getDepartmentList(null);
		List<Staff> staffList = ContextUtil.getInstance().getStaffService().getStaffList(new Staff());
		request.setAttribute("roleList",roleList);
		request.setAttribute("depotList",depotList);
		request.setAttribute("departmentList",departmentList);
		request.setAttribute("staffList",staffList);
		request.setAttribute("staff",staff);
		return mapping.findForward("modify");
	}
	
	public ActionForward staffPassword(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Staff staff = SessionValue.getInstance(request).getStaff() ;
		staff = ContextUtil.getInstance().getStaffService().getStaffById(staff.getStaffId());
		request.setAttribute("staff",staff);
		return mapping.findForward("modifyPassword");
	}
	
	public ActionForward modifyStaffPassword(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Staff staff = new Staff();
		WebUtil.setRequestToObject(request,staff);
		IStaffService staffService = ContextUtil.getInstance().getStaffService();
		staff = staffService.getStaffById(staff.getStaffId());
		String password = request.getParameter("tmpPassword");
		staff.setTmpPassword(password);
		staffService.modifyStaffPassword(staff);
		WebUtil.setAlertMessage(request,"密码已修改！");
		return staffPassword(mapping,form,request,response);
	}
	
	public ActionForward deleteStaff(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Staff staff = new Staff();
		WebUtil.setRequestToObject(request,staff);
		IStaffService service = ContextUtil.getInstance().getStaffService();
		staff = service.getStaffById(staff.getStaffId());
		if(null != staff){
			service.deleteStaff(staff);
			if(AppConstant.BASE_BEAN_STATUS_ERROR == staff.getStatus()){
				WebUtil.setAlertMessage(request,staff.getMsg());
			}
		}
		return getStaffList(mapping,form,request,response);
	}
	
	public ActionForward selectStaffTop(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		return mapping.findForward("selectListTop");
	}
	
	public ActionForward selectStaffList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
//		Staff currentStaff = SessionValue.getInstance(request).getStaff();
//		Set<Customer> set=currentStaff.getCustomers();
		Staff staff = new Staff();
		WebUtil.setRequestToObject(request,staff);
		IStaffService service = ContextUtil.getInstance().getStaffService();
		int page = 1;
		int count = 0;
	    if (null != request.getParameter(Pagination.PAGE_ATTRIBUTE)) {
	      page = Integer.parseInt(request.getParameter(Pagination.
	          PAGE_ATTRIBUTE));
	    }
	    count = (int)service.getStaffCount(staff).longValue();
	    Pagination pagination = new Pagination();
	    pagination.setPageCount(Pagination.PAGE_RECORD_LINE_NUM);
	    pagination.generatePageJSPFragment(request, count);
	    staff.setPage(page, Pagination.PAGE_RECORD_LINE_NUM);
	    Long staffId=SessionValue.getInstance(request).getStaff().getStaffId();
		List<Staff> staffList = service.getStaffList(staff);
		
		request.setAttribute("staffList",staffList);
		return mapping.findForward("selectList");
	}
}
